package main

import "fmt"

func goFunction() {

	a := []int{1, 2, 3}
	// 函数参数是值传递
	funcArray(a)
	funcSlice(a...)

	fmt.Println("CallFunc:", CallFunc())
	fmt.Println("deferReturn:", deferReturn())

	// 匿名函数，函数变量
	addFunc := func(a int, b int) int { return a + b }
	sum := addFunc(1, 2)
	fmt.Println("sum:", sum)

	testClosure()

}

func funcArray(a []int) {
	fmt.Println(a)
}

func funcSlice(a ...int) {
	funcArray(a)
}

func funcMulti() (s0 string, s1 string) {
	return "0", "1"
}

func funcEmptyInterface(a ...interface{}) {
	for i, s := range a {
		fmt.Println(i, s)
	}
}

func deferFunc(s *string) {
	fmt.Println("deferFunc:", *s)
	v := "deferFunc"
	s = &v
}

func returnFunc(s *string) string {
	fmt.Println("returnFunc:", *s)
	v := "returnFunc"
	s = &v
	return *s
}

func CallFunc() string {
	s := "CallFunc"
	fmt.Println("before")
	defer deferFunc(&s) // return执行
	fmt.Println("after")
	return returnFunc(&s)
}

func deferReturn() (result int) {
	defer func() {
		result = result + result
	}()

	return 1
}

func resultFunc() (result string) {
	result = "result"
	defer func() {
		fmt.Println(result)
	}()
	// return result
	return
}

func funcName(i int, f func(int, int) int) int {
	return f(i, i*i)
}

// Closure 闭包：返回函数
func Closure(i int) func(int) int {
	return func(j int) int {
		return i + j
	}
}

func testClosure() {
	closure := Closure(10)
	fmt.Println(closure(10))

}
